
* replicates Table A15 in the online appendix

clear *

* program to calculate marginals 
capture program drop marginals
program define marginals
	args x /* x = matrix name */
	mat `x' = r(table)'
	local r = rowsof(`x')
	mat `x' = `x'[1..`r',1..1]
end

*-------------------------------------------------------------------------------------
/* American Community Survey (2015 PUMS 3-year file) */
*-------------------------------------------------------------------------------------

use acs2015, clear
keep if year == 2015
keep if age > 17
drop if hhincome == 9999999

* randomly select one person per household
set seed 7
gen random = runiform()
sort serial random
by serial: egen rank = rank(random)
by serial: gen first = (rank == 1)
by serial: egen all = total(first)
tab all
assert r(r) == 1
drop all first
keep if rank == 1

* white (race)

gen white = (raced == 100)
foreach x in 801 802 811 812 813 814 815 816 818 821 822 823 824 826 ///
	901 902 904 905 907 911 912 913 914 916 917 920 921 922 925 950 960 ///
	963 964 982 983 990 {
		replace white = 1 if raced == `x'
		}

* education
gen hs = (educ <= 9)
gen college = (educ == 10)
gen postgrad = (educ == 11)

* age
gen age1844 = (age < 45)
gen age4564 = (age >= 45 & age < 65)
gen age65 = (age >= 65)

* female
gen female = (sex == 2)

/* General population */

mean female ///
	white ///
	hs college postgrad ///
	age1844 age4564 age65 ///
	[pw=perwt] 
marginals acs_all

/* High-income */

mean female ///
	white ///
	hs college postgrad ///
	age1844 age4564 age65 ///
	[pw=perwt] if hhincome > 250000
marginals acs_rich

*-------------------------------------------------------------------------------------
/* Our YouGov survey */
*-------------------------------------------------------------------------------------

use survey, clear

* education
gen hs = (educ == 1 | educ == 2 | educ == 3 | educ == 4)
gen college = (educ == 5)
gen postgrad = (educ == 6)

* age
gen age1844 = (age < 45)
gen age4564 = (age >= 45 & age < 65)
gen age65 = (age >= 65)

/* General pop */

* unweighted
mean female ///
	white ///
	hs college postgrad ///
	age1844 age4564 age65 ///
	if wealthy == 0
marginals yg_all_uw

* weighted
mean female ///
	white ///
	hs college postgrad ///
	age1844 age4564 age65 ///
	if wealthy == 0 [pw=weight]
marginals yg_all_w

/* Wealthy */

* unweighted
mean female ///
	white ///
	hs college postgrad ///
	age1844 age4564 age65 ///
	if incomeall >= 13
marginals yg_rich_uw

* weighted
mean female ///
	white ///
	hs college postgrad ///
	age1844 age4564 age65 ///
	if incomeall >= 13 [pw=weight]
marginals yg_rich_w

* combined table
mat all_c = acs_all,yg_all_w
mat all = acs_all,yg_all_uw,yg_all_w
mat rich_c = acs_rich,yg_rich_w
mat rich = acs_rich,yg_rich_uw,yg_rich_w
mat both = all,rich
frmttable, clear
frmttable, statmat(both) substat(0) sdec(2) ///
	rtitles("Female" \ "White" \ "High school or less" \ "College" \ "Post-graduate" \ ///
	"Aged 18-44" \ "Aged 45-64" \ "Aged 65 or more") ///
	ctitles("","General Population","","","Affluent","","" \ ///
	"","ACS","Our survey","Our survey","ACS","Our survey","Our survey" \ ///
	"","","unweighted","weighted","","unweighted","weighted") ///
	multicol(1,2,3;1,5,3) 
